package see.fa.texttransformer;

import org.junit.runner.RunWith;

import com.googlecode.instinct.marker.annotate.BeforeSpecification;
import com.googlecode.instinct.marker.annotate.Specification;
import com.googlecode.instinct.marker.annotate.Subject;
import static com.googlecode.instinct.expect.Expect.expect;
import com.googlecode.instinct.integrate.junit4.InstinctRunner;

@RunWith(InstinctRunner.class)
public class TextTransformerSpec {

    @Subject TextTransformer textTransformer;
    
    @BeforeSpecification
    void before() {
        textTransformer = new TextTransformer();
    }
    
    @Specification
    void shouldExecuteScriptAgainstText() {
        String givenSource = "dummy source.";
        TransformDetails givenTransformDetails = new TransformDetails(
                "groovy", 
                "source.set(source.get() + '-new')");
        
        String tansformedText = textTransformer.execute(givenSource, givenTransformDetails);
        
        expect.that(tansformedText).isEqualTo(givenSource + "-new");
    }
    
    @Specification(expectedException = TextTransformationException.class, reason = "Should have had an error for incorrect script.")
    void shouldThrowExceptionForIncorrectScriptTransformation() {
        String givenSource = "dummy source.";
        TransformDetails givenTransformDetails = new TransformDetails(
                "groovy", 
                "throw new RuntimeException('this always gets thrown')");
        
        textTransformer.execute(givenSource, givenTransformDetails);
    }
}
